package com.cloud.wms.ui.sort.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.main.entity.LoginCache;
import com.cloud.wms.ui.sort.contract.DetachContact;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;
import com.cloud.wms.ui.sort.entity.DetachDetailBean;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/3/8.
 * Desc：二次分拨
 */
public class DetachPresenter extends BaseRxPresenter<DetachContact.View> implements DetachContact.Presenter {
    public static final int GET_BATCH_DETAIL_SUCCESS = 0x100;
    public static final int CHECK_BOX_SUCCESS = 0x101;
    public static final int BATCH_BIND_BOX_SUCCESS = 0x102;
    public static final int CONFIRM_DETACH_SUCCESS = 0x103;

    @Inject
    public DetachPresenter() {

    }

    /**
     * 获取波次明细
     *
     * @param batchCode
     */
    @Override
    public void loadBatchDetail(String batchCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("OrderCode", batchCode);
        FineExApplication
                .component()
                .http()
                .loadBatchDetail(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<DetachDetailBean>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<DetachDetailBean> detailBeanList) {
                        if (detailBeanList.size() == 0) {
                            mView.info("没有查询到符合条件的数据");
                            return;
                        }
                        mView.onSuccess(MessageCreator.createMessage(detailBeanList, GET_BATCH_DETAIL_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 检测容器是否可用
     *
     * @param boxCode
     */
    @Override
    public void checkBox(String boxCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("BoxCode", boxCode);
        FineExApplication
                .component()
                .http()
                .checkBox(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<DetachBoxBean>(mView.provideContext()) {
                    @Override
                    public void onSuccess(DetachBoxBean detachBoxBean) {
                        mView.onSuccess(MessageCreator.createMessage(detachBoxBean, CHECK_BOX_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 容器绑定波次
     *
     * @param batchId
     * @param boxBeans
     */
    @Override
    public void batchBindBox(String batchId, List<DetachBoxBean> boxBeans) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("lstBox", boxBeans);
        params.put("userID", LoginCache.getInstance().getUserInfo().getUserID());
        params.put("orderType", 2);//1 入库单 2发货单 3波次单
        FineExApplication
                .component()
                .http()
                .batchBindBox(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Boolean>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Boolean b) {
                        mView.onSuccess(MessageCreator.createMessage(boxBeans, BATCH_BIND_BOX_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 确认分拨
     *
     * @param batchID
     */
    @Override
    public void confirmDetach(String batchID, List<DetachBoxBean> boxBeanList) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("userID", LoginCache.getInstance().getUserInfo().getUserID());
        params.put("batchID", batchID);
//        params.put("lstBindBox", boxBeanList);
        FineExApplication
                .component()
                .http()
                .confirmDetach(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object s) {
                        mView.onSuccess(MessageCreator.createMessage(s, CONFIRM_DETACH_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
